<?php
/**
 * Created by PhpStorm.
 * User: liu
 * Date: 2016/4/30
 * Time: 9:19
 */

namespace Admin\Controller;
use Think\Controller;
use Think\Page;

class UserController extends Controller
{

    function __construct()
    {
        parent::__construct();
        $this->check_auth(); //检查权限

        $sh_class = M("shclass");
        $sh_class_array = $sh_class->select();
        $sh_grade = M("grade");
        $sh_grade_array = $sh_grade->where("is_inschool=0")->select();

        $this-> assign('sh_class_array',$sh_class_array);
        $this-> assign('sh_grade_array',$sh_grade_array);

    }

     public function showAll()
    {

       $user=M('user');
       import('Org.Util.Page');
        $total=$user->count();//记录总数
        $num_pre_page=15;//每页显示8条记录
        $page=new Page($total,15);
        $page->setConfig("header","个用户");
        $pageControl=$page->show();
        $map = array();
        $map['school_id'] = $_SESSION['school_id'];  //只能查看本校的
        $users = $user
            ->alias('t')
            ->field('t.*,c.name as classname,g.name as gradename')
            ->join('left join '.C('DB_PREFIX').'shclass as c on c.id = t.class')
            ->join('left join '.C('DB_PREFIX').'grade as g on g.id = t.grade')
            ->where($map)
            ->order("t.id DESC")
            ->limit("$page->firstRow,$page->listRows")
            ->select();
     //   echo $user->_sql();

   //处理班级主任年级主任问题
        foreach($users as $k=>$userinfo)
        {
            $users[$k]['role']= $userinfo['role']==1?'班主任':'年级主任';
        }
        $this-> assign('page_title',"用户角色列表");
        $this->assign('pageControl', $pageControl);
        $this->assign('users',$users);
        $this->display();

    }
    public function showAdd()
    {
        $sh_grade = M("grade");
        $sh_class = M("shclass");
        $sh_class_array = $sh_class->select();
        $sh_grade_array = $sh_grade->where("is_inschool=0")->select();
        $teacher_class = M("teacher");
        $map['school_id'] = $_SESSION['school_id'];  //只能查看本校的
        $teacher_array = $teacher_class ->where($map)->select();
        $this->assign('teacher_array',$teacher_array);
        $user_list['role'] = 1;
        $this->assign('sh_class_array',$sh_class_array);
        $this->assign('user_list',$user_list);
        $this->assign('sh_grade_array',$sh_grade_array);
        $this-> assign('page_title',"添加");
        $this->display('update');
    }

    //实现学生修改功能
    public function update()
    {
        header("Content-Type:text/html;charset=utf-8");
        $stu=M('user');
        $id=$_GET['id'];
        if($id){
            //修改
            if($_POST['password']!=""){
                $_POST['password'] = md5($_POST['password']);
            }else{
                unset($_POST['password']);
            }
            $stu_list=$stu->where("id=$id")->save($_POST);
            if($stu_list){
                $this->success('修改成功！！');
                $this->redirect('showAll');
            }else{
                $this->error('修改失败！！');
            }
        }else{
            $stuinfo['username']=$_POST['username'];
            $stuinfo['password']=md5($_POST['password']);
            $stuinfo['role']=$_POST['role'];
            $stuinfo['class']=$_POST['class'];
            $stuinfo['grade']=$_POST['grade'];
            $result = $stu->add($stuinfo);
            if($result)
            {
                $this->success('添加成功！！');
                $this->redirect('showAll');
            }else{
                $this->error('添加失败！！');
            }
        }

    }

    public function showUpdate()
    {
        $stu=M('user');
        $id=$_GET['id'];
        $user_list=$stu->where("id=$id")->find();
        $map['school_id'] = $_SESSION['school_id'];  //只能查看本校的
        $sh_class = M("shclass");
        $sh_class_array = $sh_class->where($map)->select();

        $teacher_class = M("teacher");

        $map['school_id'] = $_SESSION['school_id'];  //只能查看本校的
        $teacher_array = $teacher_class ->where($map)->select();

        $this->assign('teacher_array',$teacher_array);

        $sh_grade = M("grade");
        $sh_grade_array = $sh_grade->where("is_inschool=0")->select();

        $this->assign('sh_class_array',$sh_class_array);
        $this->assign('sh_grade_array',$sh_grade_array);


        $this->assign('user_list',$user_list);
        $this-> assign('page_title',"修改");

        $this->display('update');
    }

    //实现学生删除功能
    public function del()
    {
        $stu=M('user');
        $id=$_GET['id'];
        $stu->where("id=$id")->delete();
        $this->redirect('User:showAll');//重定向到控制器的方法  display是本控制的方法
    }

}